package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class Test5 {
    public static int countSegments(String s) {
        //记录单词个数
        int count = 0;
        //false表示是空格或者刚开始
        boolean tmp = false;
        //遍历数组
        for(int i = 0;i<s.length();i++){
            //判断当前字符是否为空
            if(s.charAt(i) != ' '){
                //判断前一个字符是否为空格
                if(!tmp){
                    //前一个字符为空格，在前面是一个单词，count+1，将tmp的值变成true
                    tmp = true;
                    count++;
                }
            }else{
                //这个字符是空格
                tmp = false;
            }

        }
        return count;
    }
    public static void main(String[] args) {
        String s ="Hello, my name is John";
        int count = countSegments(s);
        System.out.println(count);
    }
}
